RailsCasts Pro episodes are now free!
Learn more or hide this
Note: This solution is out of date. See episode 196.
Resources
def new @project = Project.new 3.times { @project.tasks.build } end def create @project = Project.new(params[:project]) if @project.save flash[:notice] = "Successfully created project." redirect_to projects_path else render :action => 'new' end end
def task_attributes=(task_attributes) task_attributes.each do |attributes| tasks.build(attributes) end end
<% form_for :project, :url => projects_path do |f| %> <p> Name: <%= f.text_field :name %> </p> <% for task in @project.tasks %> <% fields_for "project[task_attributes][]", task do |task_form| %> <p> Task: <%= task_form.text_field :name %> </p> <% end %> <% end %> <p><%= submit_tag "Create Project" %></p> <% end %>